package serv

import (
	"gorm.io/gorm"
	"server/internal/app/model"
	"server/internal/database"
	"time"
)

var UserGroup = new(userGroup)

type userGroup struct {
	model.UserGroup
}

// Reads 列表
func (a userGroup) Reads(Db *gorm.DB) (*gorm.DB, []userGroup, int64) {
	var list []userGroup
	var total int64
	Db.Model(&a).Find(&list).Offset(-1).Count(&total)
	return Db, list, total
}

// Read 单条
func (a userGroup) Read(id any) (*gorm.DB, userGroup) {
	Db := database.DB
	Db.Model(&a).Where("id =?", id).First(&a)

	return Db, a

}

// Create 新增
func (userGroup) Create(data model.UserGroup) *gorm.DB {
	Db := database.DB
	t := time.Now()
	data.CreateTime = int(t.Unix())
	data.UpdateTime = int(t.Unix())

	return Db.Create(&data)
}

// Update 编辑
func (userGroup) Update(data model.UserGroup) *gorm.DB {
	Db := database.DB
	t := time.Now()
	data.UpdateTime = int(t.Unix())

	return Db.Updates(&data)

}

// Delete 删除
func (a userGroup) Delete(ids []int) *gorm.DB {
	Db := database.DB

	return Db.Delete(&a, ids)

}
